<?php
require_once('./admin/connexion.php');
require ('./func.req.php');
if ($install) {
    header("Location:./install/index.php");
} else {
    require('./config/tbl.php');
    if ($line_info['maintenance_choix_info'] == "Oui") {
        show_maintenance();
    } else {


        /*
          -----------------------------------
          ---- SCRIPT D'AUTHENTIFICATION ----
          FujiCraft V3.0
          By Fujido
          Contact : thefujido@gmail.com
          -----------------------------------
         */

        session_start(); // d�but de session

        if (isset($_POST['login'])) { // execution apres envoi du formulaire
            $login = mysql_escape_string($_POST['login']); // mise en variable du nom d'utilisateur
            $pass = md5($_POST['pass']); // mise en variable du mot de passe crypt�
// requete sur la table administrateurs (on r�cup�re les infos de la personne)
            mysql_select_db($database, $dbconnect);
            $verif_query = sprintf("SELECT * FROM " . $database . "." . $tb_utilisateurs . " WHERE login='$login' AND pass='$pass'"); // requ�te sur la base administrateurs
            $verif = mysql_query($verif_query, $dbconnect) or die(mysql_error());
            $row_verif = mysql_fetch_assoc($verif);
            $utilisateur = mysql_num_rows($verif);


            if ($utilisateur) { // On test s'il y a un utilisateur correspondant
                @session_register("authentification"); // enregistrement de la session
// declaration des variables de session
                $_SESSION['privilege'] = $row_verif['privilege']; // le privil�ge de l'utilisateur (permet de d�finir des niveaux d'utilisateur)
                $_SESSION['nom'] = $row_verif['nom']; // Son nom
                $_SESSION['prenom'] = $row_verif['prenom']; // Son Pr�nom
                $_SESSION['login'] = $row_verif['login']; // Son Login
                $_SESSION['pass'] = $row_verif['pass']; // Son mot de passe
                $_SESSION['email'] = $row_verif['email']; // Son email
                $_SESSION['sexe'] = $row_verif['sexe']; // Son sexe
                $_SESSION['question_secrete'] = $row_verif['question_secrete']; // Sa question secrete
                $_SESSION['reponse_secrete'] = $row_verif['reponse_secrete']; // Sa reponse secrete
                $_SESSION['date_naissance'] = $row_verif['date_naissance']; // Sa date de naissance
                $_SESSION['skype'] = $row_verif['skype']; // Son skype
                $_SESSION['date_inscription'] = $row_verif['date_inscription']; // Sa date d'inscription
                $_SESSION['nb_message'] = $row_verif['nb_message']; // son nombre de message

                header("Location:index.php"); // redirection si OK
            } else {
                header("Location:login.php?erreur=login"); // redirection si utilisateur non reconnu
            }
        }

// GESTION DE LA Deconnexion
        if (isset($_GET['erreur']) && $_GET['erreur'] == 'logout') { // Test sur les param�tres d'URL qui permettront d'identifier un "contexte" de d�connexion
            $prenom = $_SESSION['prenom']; // On garde le pr�nom en variable pour dire au revoir (soyons polis :-)
            session_unset("authentification");
            header("Location:login.php?erreur=delog");
        }
        ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <title><?php echo $site; ?> | Connexion</title>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <meta http-equiv="Content-Style-Type" content="text/css" />
                <meta http-equiv="content-language" content="fr,en" />
                <link rel='shortcut icon' type="image/x-icon" href='./images/favicon.ico' />
                <link rel="stylesheet" type="text/css" href="./css/style.css" title="" media="all" />
                <link rel="stylesheet" type="text/css" href="./css/news.css" title="" media="all" />
                <link rel="stylesheet" type="text/css" href="./css/connexion.css" title="" media="all" />
                <script type="text/javascript" src="./js/news.js"></script>
            </head>
            <body>
                <div id="block">
                    <div id="contentblock">
                        <div class="wrapper">
                            <?php
                            show_header();
                            show_menu();
                            show_newsbox();
                            show_userbox();
                            ?>
                            <div class="mainbox">
                                <h2>Connexion</h2>
                                <center>
                                    <?php
                                    if (isset($_GET['step']) == "1") {
                                        if (isset($_GET['step'])) {
                                            $step = $_GET['step'];
                                        } else {
                                            $step = 0;
                                        }
                                        switch ($step) {
                                            case 1:
                                                ?>
                                                <form id="start" action="<?php echo $_SERVER['PHP_SELF']; ?>?step=2" method="post">
                                                    <h1>R&eacute;initialisation du mot de passe</h1>
                                                    <p>
                                                        <label for="email">Votre e-mail</label>
                                                        <input id="email" name="email" type="text" />
                                                    </p>
                                                    <p>
                                                        <label for="question">Question</label>
                                                        <select id="question" name="question">
                                                            <option><?php echo $question_secrete1; ?></option>
                                                            <option><?php echo $question_secrete2; ?></option>
                                                            <option><?php echo $question_secrete3; ?></option>
                                                            <option><?php echo $question_secrete4; ?></option>
                                                            <option><?php echo $question_secrete5; ?></option>
                                                        </select>
                                                    </p>
                                                    <p>
                                                        <label for="reponse">R&eacute;ponse</label>
                                                        <input id="reponse" name="reponse" type="text" />
                                                    </p>
                                                    <p>
                                                        <input type="submit" name="submit" value="Envoyer" />
                                                    </p>
                                                </form>
                                                <?php
                                                break;
                                            case 2:
                                                $email_test = mysql_escape_string(@$_POST['email']);
                                                $query_pass = "SELECT * FROM " . $database . "." . $tb_utilisateurs . " WHERE email = '$email_test'  ";
                                                $result_pass = @mysql_query($query_pass) OR DIE("Erreur de la s&eacute;lection des informations de l'utilisateurs : " . mysql_error());
                                                $line_pass = mysql_fetch_assoc($result_pass);
                                                $question_test = @$_POST['question'];
                                                $reponse_test = utf8_encode(@$_POST['reponse']);
                                                $question_ok = $line_pass['question_secrete'];
                                                $reponse_ok = $line_pass['reponse_secrete'];
                                                if (preg_match("#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#", $_POST['email']) && $question_test == $question_ok && $reponse_test == $reponse_ok) {
                                                    ?>
                                                    <form id="start" action="<?php echo $_SERVER['PHP_SELF'] . "?step=3"; ?>" method="post">
                                                        <h1>Cr&eacute;ation du nouveau mot de passe</h1>
                                                        <p>
                                                            <label for="newpass">Mot de passe</label>
                                                            <input id="newpass" name="newpass" type="password" />
                                                        </p>
                                                        <p>
                                                            <label for="newpass2">Retapez-le</label>
                                                            <input id="newpass2" name="newpass2" type="password" />
                                                            <input type="hidden" name="email_test" value="<?php echo $email_test; ?>" />
                                                        </p>
                                                        <p>
                                                            <input type="submit" name="submit" value="Envoyer" />
                                                        </p>
                                                    </form>
                                                    <?php
                                                } else {
                                                    ?>
                                                    <form id="start" action="<?php echo $_SERVER['PHP_SELF']; ?>?step=1" method="post">
                                                        <h1>R&eacute;initialisation du mot de passe</h1>
                                                        <p>
                                                            <label for="email">Votre e-mail</label>
                                                            <input id="email" name="email" type="text" />
                                                        </p>
                                                        <p>
                                                            <label for="question">Question</label>
                                                            <select id="question" name="question">
                                                                <option><?php echo $question_secrete1; ?></option>
                                                                <option><?php echo $question_secrete2; ?></option>
                                                                <option><?php echo $question_secrete3; ?></option>
                                                                <option><?php echo $question_secrete4; ?></option>
                                                                <option><?php echo $question_secrete5; ?></option>
                                                            </select>
                                                        </p>
                                                        <p>
                                                            <label for="reponse">R&eacute;ponse</label>
                                                            <input id="reponse" name="reponse" type="text" />
                                                        </p>
                                                        <div id="box" onclick="document.getElementById('box').style.display='none';" class="error-box" title="Fermer la fen&ecirc;tre">
                                                            Erreur lors de la v&eacute;rification.
                                                        </div>
                                                        <p>
                                                            <input type="submit" name="submit" value="Envoyer" />
                                                        </p>
                                                    </form>
                                                    <?php
                                                }
                                                break;
                                            case 3:
                                                if (isset($_POST['submit']) && $_POST['email_test'] != "" && $_POST['newpass'] == $_POST['newpass2']) {
                                                    $email_user = $_POST['email_test'];
                                                    $newpass = md5($_POST['newpass']);
                                                    $query_pass = "UPDATE " . $database . "." . $tb_utilisateurs . " SET pass = '$newpass' WHERE email = '$email_user' ";
                                                    $result_pass = @mysql_query($query_pass) OR DIE("Erreur de la s&eacute;lection des informations de l'utilisateurs : " . mysql_error());
                                                    ?>
                                                    <form id="start" action="" method="post" name="connect">
                                                        <h1>Connexion</h1>
                                                        <p>
                                                            <label for="login">Pseudo</label>
                                                            <input id="login" name="login" type="text" />
                                                        </p>
                                                        <p>
                                                            <label for="pass">Mot de passe</label>
                                                            <input id="pass" name="pass" type="password" />
                                                        </p>
                                                        <p>
                                                            <input type="submit" name="submit" value="Envoyer" />
                                                        </p>
                                                        <div id="box" onclick="document.getElementById('box').style.display='none';" class="success-box" title="Fermer la fen&ecirc;tre">
                                                            Changement de mot de passe effectu&eacute;.
                                                        </div>
                                                    </form>
                                                    <?php
                                                } else {
                                                    ?>
                                                    <form id="start" action="<?php echo $_SERVER['PHP_SELF'] . "?step=2"; ?>" method="post">
                                                        <h1>Cr&eacute;ation du nouveau mot de passe</h1>
                                                        <p>
                                                            <label for="newpass">Mot de passe</label>
                                                            <input id="newpass" name="newpass" type="password" />
                                                        </p>
                                                        <p>
                                                            <label for="newpass2">Retapez-le</label>
                                                            <input id="newpass2" name="newpass2" type="password" />
                                                            <input type="hidden" name="email_test" value="<?php echo $email_test; ?>" />
                                                        </p>
                                                        <div id="box" onclick="document.getElementById('box').style.display='none';" class="error-box" title="Fermer la fen&ecirc;tre">
                                                            Erreur lors de la v&eacute;rification ou lors du changement de mot de passe.
                                                        </div>
                                                        <p>
                                                            <input type="submit" name="submit" value="Envoyer" />
                                                        </p>
                                                    </form>
                                                    <?php
                                                }
                                                break;
                                        }
                                    } else {
                                        ?>
                                        <form id="start" action="" method="post" name="connect">
                                            <h1>Connexion</h1>
                                            <p>
                                                <label for="login">Pseudo</label>
                                                <input id="login" name="login" type="text" />
                                            </p>
                                            <p>
                                                <label for="pass">Mot de passe</label>
                                                <input id="pass" name="pass" type="password" />
                                            </p>
                                            <p>
                                                <input type="submit" name="submit" value="Envoyer" />
                                            </p>

                                            <?php if (isset($_GET['erreur']) && ($_GET['erreur'] == "login")) { // Affiche l'erreur  ?>
                                                <div id="box" onclick="document.getElementById('box').style.display='none';" class="error-box" title="Fermer la fen&ecirc;tre">
                                                    Echec d'authentification. Login ou mot de passe incorrect.
                                                </div>
                                            <?php } ?>
                                            <?php if (isset($_GET['erreur']) && ($_GET['erreur'] == "delog")) { // Affiche l'erreur ?>
                                                <div id="box" onclick="document.getElementById('box').style.display='none';" class="success-box" title="Fermer la fen&ecirc;tre">
                                                    D&eacute;connexion r&eacute;ussi. &Agrave; bient&ocirc;t.
                                                </div>
                                            <?php } ?>
                                            <?php if (isset($_GET['erreur']) && ($_GET['erreur'] == "intru")) { // Affiche l'erreur
                                                ?>
                                                <div id="box" onclick="document.getElementById('box').style.display='none';" class="warning-box" title="Fermer la fen&ecirc;tre">
                                                    Tentative d'intrusion d&eacute;t&eacute;ct&eacute;e. Les admins ont &eacute;t&eacute; pr&eacute;venus.
                                                </div>
                                            <?php } ?>
                                            <?php if (isset($_GET['erreur']) && ($_GET['erreur'] == "needlog")) { // Affiche l'erreur
                                                ?>
                                                <div id="box" onclick="document.getElementById('box').style.display='none';" class="warning-box" title="Fermer la fen&ecirc;tre">
                                                    Ceci est une page prot&eacute;g&eacute;e. Veuillez vous connecter.
                                                </div>
                                            <?php } ?>
                                            <hr />
                                            <br />
                                            <a href="<?php echo $_SERVER['PHP_SELF']; ?>?step=1" title="Vous avez perdu ou oubli&eacute; votre mot de passe ?"><b>Mot de passe perdu ?</b></a>
                                        </form>
                                        <?php
                                    }
                                    ?>
                                </center>
                                <br />
                                <br />
                                <br />
                                <br />
                            </div>
                            <?php show_footer(); ?>
                        </div>
                    </div>
                </div>
            </body>
        </html>
        <?php
    }
}
?>